package com.ruoyi.cyinfo.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.CheckWechatToken;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cyinfo.domain.InfoIdiom;
import com.ruoyi.cyinfo.domain.InfoUserOther;
import com.ruoyi.cyinfo.mapper.InfoUserOtherMapper;
import com.ruoyi.cyinfo.service.IInfoIdiomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 成语库Controller
 *
 * @author ruoyi
 * @date 2024-03-10
 */
@RestController
@RequestMapping("/wechat/infoIdiom")
public class WeChatInfoIdiomController extends BaseController {
    @Autowired
    private IInfoIdiomService infoIdiomService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private InfoUserOtherMapper infoUserOtherMapper;

    /**
     * 开始闯关
     * @return
     */
    @CheckWechatToken
    @GetMapping("/select")
    public AjaxResult wXselectIdiom(){
        //1.再导入成语或对成语修改时去跟新redis中的数据
        //2.给前端返回所有题库数据，前端排序展示
        InfoUserOther userOther =  infoUserOtherMapper.selectInfoUserOtherByUserId(SecurityUtils.getUserId());
        List<Long> longs = JSON.parseArray(userOther.getAnswerId(), Long.class);
        //查询没有答过的第一条数据
        InfoIdiom infoIdiom =  infoIdiomService.selectInfoIdiomByRand(userOther.getAnswerId().substring(1,userOther.getAnswerId().length()-1));
        //3.用户答题分为三种情况  对 错 退出
        //对的话掉接口存储题目id标记 减去体力，错的话继续答题-体力，退出就调用退出接口跟新mysql
        return AjaxResult.success(infoIdiom);
    }

    @CheckWechatToken
    @PostMapping("/next")
    public AjaxResult wXnext(InfoIdiom infoIdiom){
        InfoUserOther userOther = infoUserOtherMapper.selectInfoUserOtherByUserId(SecurityUtils.getUserId());
        if (userOther.getStrengthValue() > 0){
            if (StringUtils.isNull(infoIdiom.getOption1())){
                //减去体力-1
                userOther.setStrengthValue(userOther.getStrengthValue()-1);
                return AjaxResult.error(202,"回答错误！");
            }else {
                //答对体力-1
                userOther.setStrengthValue(userOther.getStrengthValue()-1);
                //存入答题数+1
                userOther.setAnswerCount(userOther.getAnswerCount()+1);
                //记录历史答过题目
                List<Long> ids;
                if (StringUtils.isNull(userOther.getAnswerId())){
                    ids = new ArrayList<>();
                }else {
                    ids = JSON.parseArray(userOther.getAnswerId(), Long.class);
                }
                ids.add(infoIdiom.getId());
                String idsString = JSON.toJSONString(ids);
                userOther.setAnswerId(idsString);
                return AjaxResult.success("回答正确！");
            }
        }
        return AjaxResult.error(201,"体力不足！");

    }

}
